const fs = require('fs');
const path = require('path');

function myplugin02(options) {
    this.options = JSON.parse(JSON.stringify(options));
}

myplugin02.prototype.apply = function(compiler) {
    const { options } = this;
    console.log(options);
	compiler.hooks.emit.tapAsync('myplugin02', (compilation, cb) => {
		console.log('webpack 构建过程开始！');
		fs.readFile(options.template, (err, data) => {
            if (err) throw err
            var html = data.toString().replace('<!-- built files will be auto injected -->', '<script src="./main.js"></script>');

            fs.writeFile(options.filename, html, function () {
                console.log('模版写入成功')
                cb()
            })
        })
	});
};

module.exports = myplugin02;